const db = require('../config/db');

const CategoryModel = {
  // 获取所有分类
  getAllCategories: async () => {
    const [rows] = await db.execute('SELECT * FROM categories');
    return rows;
  },
  
  // 获取分类详情
  getCategoryById: async (id) => {
    const [rows] = await db.execute('SELECT * FROM categories WHERE id = ?', [id]);
    return rows[0] || null;
  },
  
  // 创建分类
  createCategory: async (name, description, image) => {
    const [result] = await db.execute(
      'INSERT INTO categories (name, description, image) VALUES (?, ?, ?)',
      [name, description, image]
    );
    return result.insertId;
  },
  
  // 更新分类
  updateCategory: async (id, updateData) => {
    const { name, description, image } = updateData;
    const [result] = await db.execute(
      'UPDATE categories SET name = ?, description = ?, image = ? WHERE id = ?',
      [name, description, image, id]
    );
    return result.affectedRows > 0;
  },
  
  // 删除分类
  deleteCategory: async (id) => {
    const [result] = await db.execute('DELETE FROM categories WHERE id = ?', [id]);
    return result.affectedRows > 0;
  }
};

module.exports = CategoryModel;